/*
 *      gravsim.cpp
 *      
 *      Copyright 2008 Antti Leivo <skylark@skylark-desktop>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include <ClanLib/core.h>
#include <ClanLib/display.h>
#include <ClanLib/gl.h>
#include <ClanLib/mikmod.h>
#include <ClanLib/sound.h>
#include <cstdlib>

#include "gravsimGUI.h"
#include "gravsim.h"
#include "stone.h"

Gravsim app;

int Gravsim::main(int argc, char** argv)
{
	srand(clock());
	World w;

	/* basic setup*/
	//Stone s1(StoneForm(180,1200), 0, 0, 0, 0);
	//Stone s2(25, 150, 5, 0, 0);
	//Stone s3(10, 90, -90, 0, 0);
	//w.insertStone(&s1);
	//w.insertStone(&s2);
	//w.insertStone(&s3);

	/* random setup */
	for (unsigned int i=0; i<6; i++)
	{
		float _galaxy_x = Gravsim::random(-3800,3800);
		float _galaxy_y = Gravsim::random(-3800,3800);
		float _galaxy_vx = Gravsim::random(-15,15)-0.05*_galaxy_x;
		float _galaxy_vy = Gravsim::random(-15,15)-0.05*_galaxy_y;
		for (unsigned int j=0; j<35; j++)
		{
			StoneForm _form;
			if (Gravsim::random(0,1) < 0.1)
			{
				_form = StoneForm(
					/* box w*/Gravsim::random(15, 80),
					/* box h*/Gravsim::random(15, 80));
			}
			else
			{
				_form = StoneForm(/*ball radius*/Gravsim::random(10, 20));
			}
			Stone *s = new Stone(
			/*form*/_form,
			/*x*/   _galaxy_x + Gravsim::random(-500, 500),
			/*y*/   _galaxy_y + Gravsim::random(-500, 500),
			/*velx*/_galaxy_vx + Gravsim::random(-50, 50),
			/*vely*/_galaxy_vy + Gravsim::random(-50, 50),
			/*angl*/Gravsim::random(-3.14159, 3.14159),
			/*avel*/Gravsim::random(-1, 1));
			w.insertStone(s);
		}
	}
	if (w.getStatus() == sysRunnable)
	{
		GravsimGUI gui(&w);
		gui.run();
	}
	else
	{
		std::cout << "GRAVSIM::Error: World not runnable." << std::endl;
	}
    return 0;
}


float Gravsim::random(float min, float max)
{
	return min + ((float)rand()/(float)RAND_MAX)*(max-min);
}
